package com.huluobo.a2303mdmdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.lib_base.RoutePath
import com.huluobo.a2303mdmdemo.pages.MainPage
import com.huluobo.a2303mdmdemo.pages.SplashPage
import com.huluobo.a2303mdmdemo.pages.WelcomePage
import com.huluobo.a2303mdmdemo.ui.theme._2303MDMDemoTheme
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.POST_NOTIFICATIONS
            ), 102
        )
        setContent {
            _2303MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Scaffold { paddingValues ->
                    Column(modifier = Modifier.padding(paddingValues = paddingValues)) {
                        //默认起始页为欢迎页
                        var startPage = RoutePath.WELCOME
                        //获取mmkv
                        val mmkv = MMKV.defaultMMKV()

                        //首页和欢迎页单独使用一个navHostController,和MainPage的导航不是一个
                        val navHostController = rememberNavController()

                        //通过mmkv取出是否进入过app
                        val isIn = mmkv.decodeBool(Const.PARAM_IS_IN)

                        if (isIn) {
                            startPage = RoutePath.MAIN
                        } else {
                            lifecycleScope.launch {
                                //通过异步来延时5秒,并且让程序进行等待
                                async { delay(5000) }.await()
                                //等待完成,跳转到首页
                                navHostController.navigate(RoutePath.SPLASH)
                                //保存进入过app
                                mmkv.encode(Const.PARAM_IS_IN, true)
                            }
                        }

                        NavHost(navController = navHostController, startDestination = startPage, builder = {
                            composable(RoutePath.WELCOME) { WelcomePage() }
                            composable(RoutePath.MAIN) { MainPage() }
                            composable(RoutePath.SPLASH) { SplashPage(navHostController = navHostController) }
                        })

                    }
                }
            }
        }
    }
}